﻿using System;
using System.Web;
using VTemplate.Engine;
using System.Collections.Generic;
using System.IO;
using System.Web.UI;
using System.Text.RegularExpressions;
using System.Xml;
using System.Data;

public class CreateCrm : BasePage
{
    List<Dukey.Model.Boards> listb = BLL.Boards.instance.GetList("isshow=True and ismain=True");//版块列表
    List<Dukey.Model.Articles> listArticle = BLL.Articles.instance.GetListByCache("");//文章列表
    Dukey.Model.WebConfig mysite = BLL.WebConfig.instance.GetModelByCache();//网站配置信息

    #region 解析模板
    protected override void InitPageTemplate()
    {
        
        try
        {
            if (mysite.islock)
            {
                if (string.IsNullOrEmpty(mysite.closepage))
                {
                    Response.Write("该网站已关闭!");
                }
                else
                {
                    //这里的页面不能用解析的.                 
                    Response.Redirect(mysite.closepage);
                }
                Response.End();
            }
        }
        catch { }

        //设置地址栏参数
        this.Document.Variables.SetValue("url", Request.QueryString);
        this.Document.Variables.SetValue("mysite", mysite);
        this.Document.Variables.SetValue("basepath", BaseWeb.BaseUrl + "/template/crm/");



    }
    #endregion

    /// <summary>
    /// 获取子级版块
    /// </summary>
    /// <param name="fid"></param>
    /// <returns></returns>
    public static List<Dukey.Model.Boards> GetBoards()
    {
        TemplateDocument td = TemplateDocument.CurrentRenderingDocument;
        if (td != null)
        {
            if (td.CurrentRenderingTag != null)
            {
                Tag tag = td.CurrentRenderingTag;
                int fid = 0;
                int.TryParse(tag.Attributes.GetValue("fid"), out fid);
                List<Dukey.Model.Boards> listc = BLL.Boards.instance.GetList("isshow=True and ismain=True");//版块列表
                return listc.FindAll(item => item.fid == fid);
            }
        }
        return null;
    }


    protected override void LoadTemplateFile(string fileName)
    {
        //Response.Write("<div>"+Request.Url.AbsolutePath+"</div>");
        //GetConfig();
        string action = Request.QueryString["fn"];
        
        if (!StringHelper.IsBadChar(action))
        {
            fileName = this.Server.MapPath("~/template/crm/" + action + ".html");
            base.LoadTemplateFile(fileName);
        }
        else
        {
            fileName = this.Server.MapPath("~/template/crm/index.html");
            base.LoadTemplateFile(fileName);
        }
    }




    #region 通用获取数据方法
    /// <summary>
    /// 通用获取数据方法
    /// </summary>
    /// <returns></returns>
    public static DataTable GetData()
    {
        TemplateDocument td = TemplateDocument.CurrentRenderingDocument;
        if (td != null)
        {
            if (td.CurrentRenderingTag != null)
            {
                string filename = "index";
                if (string.IsNullOrEmpty(System.Web.HttpContext.Current.Request.QueryString["fn"]))
                {
                    filename = System.Web.HttpContext.Current.Request.QueryString["fn"];
                }
                string bid = System.Web.HttpContext.Current.Request.QueryString["bid"];
                Tag tag = td.CurrentRenderingTag;
                //string vard = tag.Attributes.GetValue("var");
                string tablename = tag.Attributes.GetValue("data");
                string filedshow = tag.Attributes.GetValue("show");
                int top = 0;
                int.TryParse(tag.Attributes.GetValue("top"), out top);
                string strwhere = tag.Attributes.GetValue("filter");
                string orderstr = tag.Attributes.GetValue("px");
                try
                {
                    DataSet ds= BLL.DataBaseHelper.instance.GetList(tablename, filedshow, top, strwhere, orderstr);
                    if (ds.Tables.Count > 0)
                        return ds.Tables[0];
                    return null;                   
                }
                catch
                {
                    return null;
                }
            }
        }
        return null;
    }
    #endregion


    #region 获取单条记录
    /// <summary>
    /// 通用获取数据方法
    /// </summary>
    /// <returns></returns>
    public static DataRowView GetSingleData()
    {
        TemplateDocument td = TemplateDocument.CurrentRenderingDocument;
        if (td != null)
        {
            if (td.CurrentRenderingTag != null)
            {
                Tag tag = td.CurrentRenderingTag;
                string tablename = tag.Attributes.GetValue("data");
                string filedshow = tag.Attributes.GetValue("show");
                string strwhere = tag.Attributes.GetValue("filter");
                try
                {
                    return BLL.DataBaseHelper.instance.GetModelView(tablename, filedshow, strwhere);
                }
                catch
                {
                    return null;
                }
            }
        }
        return null;
    }
    #endregion

    #region 通用获取数据分页
    /// <summary>
    /// 通用获取数据方法
    /// </summary>
    /// <returns></returns>
    public static DataSet GetList()
    {
        TemplateDocument td = TemplateDocument.CurrentRenderingDocument;
        if (td != null)
        {
            if (td.CurrentRenderingTag != null)
            {
                Tag tag = td.CurrentRenderingTag;
                string var = tag.Attributes.GetValue("var");
                string tablename = tag.Attributes.GetValue("data");
                string filedkey = "id";
                if(!string.IsNullOrEmpty(tag.Attributes.GetValue("key")))
                {
                    filedkey = tag.Attributes.GetValue("key");
                }
                string filedshow = tag.Attributes.GetValue("show");
                int pagesize = 2;
                int.TryParse(tag.Attributes.GetValue("pagesize"), out pagesize);
                int pageindex = 1;
                if (StringHelper.isNum(System.Web.HttpContext.Current.Request.QueryString["pn"]))
                {
                    int.TryParse(System.Web.HttpContext.Current.Request.QueryString["pn"], out pageindex);
                }
                int total = 0;
                int pagecount=1;
                string strwhere = tag.Attributes.GetValue("filter");
                string orderstr = tag.Attributes.GetValue("px");

                try
                {
                    DataSet ds = BLL.DataBaseHelper.instance.GetList(tablename, filedkey, filedshow, pagesize, pageindex, strwhere, orderstr, out total,out pagecount);                 
                    td.SetValue("total", total);
                    td.SetValue("pagecount", pagecount);
                    return ds;
                }
                catch (Exception ex)
                {
                    throw ex;
                    return null;
                }
            }
        }
        return null;
    }
    #endregion

    public static string GetAds()
    {
        TemplateDocument td = TemplateDocument.CurrentRenderingDocument;
        if (td != null)
        {
            if (td.CurrentRenderingTag != null)
            {
                Tag tag = td.CurrentRenderingTag;
                string code = tag.Attributes.GetValue("code");
                try
                {
                    object objModel = Common.DataCache.GetCache("ads_"+code);
                    if (objModel == null)
                    {
                        try
                        {
                            objModel = GetAds(code);
                            if (objModel != null)
                            {
                                Common.DataCache.SetCache("ads_" + code, objModel);
                            }
                        }
                        catch { }
                    }
                    return objModel as string;
                }
                catch
                { }

            }
        }
        return "";
    }
    private static string GetAds(string code)
    {
        try
        {       
            object o =BLL.DataBaseHelper.instance.GetSingle("ads","contents","ename='"+code+"'","");
            if (o != null)
                return o.ToString();
            //else
            //    return "广告位,编号: " + code;
        }
        catch
        { }
        return "";
    }   

}